﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyMoviesBrowserLibrary;

namespace TVManager
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        private Settings settings;
        private TVManagerBusinessLayer bl = new TVManagerBusinessLayer();
        public SettingsWindow()
        {
            InitializeComponent();
            saveSettingsButton.Click += new RoutedEventHandler(selectLanguageButton_Click);
            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            settings = new Settings();
            Loaded += new RoutedEventHandler(SettingsWindow_Loaded);
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            settings.EpisodeSearchString = Settings.Default.EpisodeSearchString;
            settings.RenameFile = Settings.Default.RenameFile;
            settings.RenameFormat = Settings.Default.RenameFormat;
            settings.MoveFile = Settings.Default.MoveFile;


            settingsGrid.DataContext = settings;

            List<Language> languages = bl.GetLanguages();
            languageListBox.ItemsSource = languages;
            if (!string.IsNullOrEmpty(bl.ChoosenLanguage))
            {
                var lan = languages.Where(l => l.Abbreviation == bl.ChoosenLanguage).FirstOrDefault();
                if (lan != null)
                    languageListBox.SelectedItem = lan;
            }
        }

        void selectLanguageButton_Click(object sender, RoutedEventArgs e)
        {
            Language language = languageListBox.SelectedItem as Language;
            if (language != null)
            {
                bl.ChoosenLanguage = language.Abbreviation;
                Settings.Default.EpisodeSearchString = settings.EpisodeSearchString;
                Settings.Default.RenameFile = settings.RenameFile;
                Settings.Default.RenameFormat = settings.RenameFormat;
                Settings.Default.MoveFile = settings.MoveFile;
                Settings.Default.Save();

                Close();
            }
        }
    }
}
